﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TCP
    {
        //public string FROMDATE = "", ENDDATE = "",  LO = "",LB = "";
       
        

        public TCP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_GBMOV();
            this.DataContext = MV; 
            InitializeComponent(); 
        }
       
        public void LOADBYGB(string partie)
        {
            try
            {
                TBMain.Clear();
                if (partie.Length < 7)
                    return;
                dwData = TBMain.NewRow();
                dwData["GABMANDANT"] = app.Mandant;
                dwData["ICCLO"] = LO;
                dwData["ICCLB"] = LB;
                dwData["ICCDAT"] = FROMDATE;
                dwData["GABBUCHDAT"] = ENDDATE;
                dwData["GABPARTIE"] = partie;
                SResult = ((VICC_GBMOV)MV).QueryICC_GBMOV(dwData, 2);
                if (SResult.Tables.Count > 0)
                {
                    string type = "";
                    foreach (DataRow dw in SResult.Tables[0].Rows)
                    {
                        type = dw["ICCTYPE"].ToString();
                        if (type == "POI")
                        {
                            dw["TYP"] = "采购入库";
                        }
                        if (type == "MUI")
                        {
                            dw["TYP"] = "手工入库";
                        }
                        if (type == "MUO")
                        {
                            dw["TYP"] = "手工调整";
                        }
                        if (type == "SMO")
                        {
                            dw["TYP"] = "免费样纱";
                        }
                        if (type == "PDI")
                        {
                            dw["TYP"] = "生产入库";
                        }
                        if (type == "STO")
                        {
                            dw["TYP"] = "发外厂或发生产";
                        }
                        if (type == "STI")
                        {
                            dw["TYP"] = "外库转入或生产退料";
                        }
                        if (type == "SOO")
                        {
                            dw["TYP"] = "销售出库";
                        }
                        if (type == "SOI")
                        {
                            dw["TYP"] = "销售退货";
                        }
                    }
                }
                MergeData(SResult, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
       
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {                
                txtDAT.Text = Helpers.Helper.GetDate(sender);
                if (dwMaster == null) return;
                dwMaster["ICCDAT"]=txtDAT.Text.ToString().Trim();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
                
        private void PrintLBCommand(object sender, RoutedEventArgs e)
        {/*
            
            bool IsFound = false;
            if (TBMain.Rows.Count == 0)
                return;
            DataTable tb = TBMain.Clone();
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;

                DataRow dwItem = ((System.Data.DataRowView)item).Row;
                DataRow _dwWB = tb.Rows.Add();
                _dwWB.ItemArray = dwItem.ItemArray;//selected
            }
            //bianli 
            foreach (DataRow row in tb.Rows)
            {
                filter = "key='"+row[].ToString()+"'";
                rows = TBMain.Select(filter);//az
                foreach (DataRow dwWB in rows)
                {
                    dwWB["date"] = "";
                }
            }*/
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                ((VICC_GBMOV)MV).BATCHGBMOVCHG();
                base.SaveCommand(sender, e);
                IsEdit = false;
                LOADBYGB(PARTIE);
                DT.Text = "";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("已处于编辑状态，请先将数据保存后才可再次进行编辑！");
                return;
            }            
            MV.TBTemp = null;
            MV.TBTemp = TBMain.Copy();
            base.EditCommand(sender, e);
            IsEdit = true;
        }
        private void DT2ALLCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            //if (dwMaster == null)
            //    return;
            //foreach (DataRow row in TBMain.Rows)
            //{
            //    row["ICCDAT"] = dwMaster["ICCDAT"];
            //}   
            string dd = txtDAT.Text.ToString().Trim();
            if (TBMain.Rows.Count == 0)
                return;
            DataTable tb = TBMain.Clone();
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;

                DataRow dwItem = ((System.Data.DataRowView)item).Row;
                DataRow _dwWB = tb.Rows.Add();
                _dwWB.ItemArray = dwItem.ItemArray;
            }            
            foreach (DataRow row in tb.Rows)
            {
                filter = "GABBELEG='" + row["GABBELEG"].ToString() + "' AND GABLFDNR ='" + row["GABLFDNR"].ToString() + "' ";
                rows = TBMain.Select(filter);
                foreach (DataRow dwWB in rows)
                {
                    dwWB["ICCDAT"] = dd;
                }
            }

        }
        public override void OnPARTIEChg(string value)
        {
            LOADBYGB(value);
            IsEdit = false;
            base.OnPARTIEChg(value);
        }

    }
}
